<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
            >
                <template slot="menuLeft">
                    <el-button
                            round
                            type="primary"
                            @click="dialogVisible = true">批量送达
                    </el-button>
                </template>
                <template slot-scope="scope" slot="menu">
                    <el-button  type="text"
                                size="small"
                                v-show="scope.row.status==='1'"
                                @click="beginDelivery(scope.row)">开始配送</el-button>
                </template>

            </avue-crud>
        </basic-container>
        <el-dialog title="批量送达" :visible.sync="dialogVisible" width="50%">
            <el-form :model="form" :inline="true" label-width="100px">
                <el-form-item label="配送日期:">
                    <el-date-picker class="form-field" v-model="form.deliveryDate" type="date" placeholder="选择日期"
                                    value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="配送批次:">
                    <el-select class="form-field"
                               v-model="form.deliverySeq"
                               placeholder="请选择">
                        <el-option v-for="item in buySeqOptions"
                                   :key="item.value" :label="item.label"
                                   :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="deliveryFinish">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<!-- 配送单记录 -->
<script>
    import {arriveBatch, fetchList, deliveryBegin} from './../../../api/scm/product/shipOrder'
    import {tableOption} from './../../../const/crud/scm/product/shiporder'
    import {getStore, setStore} from "@/util/store";
    export default {
        name: 'order',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                pmId: '',
                dialogVisible: false,
                form: {
                    deliveryDate: '',
                    deliverySeq: ''
                },
                buySeqOptions: this.$dic.buySeqOptions,
            }
        },
        created() {
            this.pmId = getStore({name: "pmId"});
        },
        computed: {},
        methods: {
            getList(page, params = {}) {
                this.tableLoading = true
                params.pmId = this.pmId;
                params.current = page.currentPage;
                params.size = page.pageSize;
                fetchList(params).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            // 确认送达
            deliveryFinish() {
                // 数据处理
                if (!this.form.deliveryDate || !this.form.deliverySeq) {
                    this.$message.warning("请选择配送日期和批次");
                    return;
                }
                let params = {
                    pmId: this.pmId,
                    deliveryDate: this.form.deliveryDate,
                    deliverySeq: this.form.deliverySeq
                }
                arriveBatch(params).then(res => {
                    if (res.data.code === 0) {
                        this.$message.success(res.data.msg);
                        this.dialogVisible = false;
                    } else {
                        this.$message.warning(res.data.msg);
                    }
                });
            },
            // 开始配送
            beginDelivery(row){
              this.$confirm('确认收货吗？', '确认信息', {
                distinguishCancelAndClose: true,
                confirmButtonText: '确认',
                cancelButtonText: '取消'
              })
                .then(() => {
                  let params = {
                    pmId: this.pmId,
                    shipId: row.shipId
                  }
                  deliveryBegin(params).then(res => {
                    if (res.data.code === 0) {
                      this.$message.success("开始配送");
                    } else {
                      this.$message.warning(res.data.msg);
                    }
                  });
                })
                .catch(action => {

                });
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
},
        }
    }
</script>

<style lang="scss" scoped>
</style>
